// pages/my/my.js
import { getUserInfo } from '../../api/user'
import Dialog from '@vant/weapp/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const token = wx.getStorageSync('token')
    if (token) {
      this.getUserInfoData()

      wx.setTabBarItem({
        index: 3,
        text: '我的'
      })
    } else {
      wx.setTabBarItem({
        index: 3,
        text: '未登录'
      })

      wx.redirectTo({
        url: '/subpkg/login/login?redirect=/pages/my/my',
      })
    }
  },
  async getUserInfoData() {
    const res = await getUserInfo()
    if (res.code === 200) {
      this.setData({
        userInfo: res.data
      })
    }
  },
  logout() {
    Dialog.confirm({
      title: '提示',
      message: '确认退出吗？',
    })
      .then(() => {
        // on confirm
        // console.log('---logout---')
        wx.removeStorageSync('token')

        wx.setTabBarItem({
          index: 3,
          text: '未登录'
        })

        // 跳转到登录页面
        wx.redirectTo({
          url: '/subpkg/login/login',
        })
      }, () => {
        console.log('---取消---')
      })
  },
  goToOrder() {
    wx.navigateTo({
      url: '/subpkg/order/order',
    })
  }
})